# Direct outputs eligible for public release

# GLOBAL SETTINGS --------------------------------------------------------------

options(
    scipen = 999,
    digits = 16,
    max.print = .Machine$integer.max,
    show.error.locations = TRUE,
    warn = 1
)

RNGkind("L'Ecuyer-CMRG")
seed <- 818675309L
set.seed(seed) # setting main seed

# PACKAGES ---------------------------------------------------------------------
library(data.table)
library(ggplot2)
library(scales)

# PACKAGE SETTINGS -------------------------------------------------------------

# data.table
setDTthreads(threads = 1L)
options(datatable.print.class = TRUE, datatable.print.keys = TRUE)
# so that printing the data.table also shows the variable type on top

# BEGIN FILE -------------------------------------------------------------------

# READ IN OUR DATA -------------------------------------------------------------

moved_tract_dt <-
    readRDS(
        "~/estimation-output/wealth_effect_estimates_moved_tract.rds"
    )
setnames(moved_tract_dt, "ref_event_time", "event_time")
moved_tract_dt <-
    moved_tract_dt[
        model == "ratio" &
        event_time %in% c(1, 96, 99) &
        income_quartile %in% c(1, 4, 5)
    ]

# Produce graph labels

moved_tract_dt[, sample_label := as.character(NA)]
moved_tract_dt[income_quartile == 5, sample_label := "Full Sample"]
moved_tract_dt[income_quartile == 1, sample_label := "Quartile 1"]
moved_tract_dt[income_quartile == 4, sample_label := "Quartile 4"]
moved_tract_dt[
    ,
    sample_label :=
        factor(
            sample_label,
            levels = c("Full Sample", "Quartile 1", "Quartile 4")
        )
]

moved_tract_dt[, time_window_label := as.character(NA)]
moved_tract_dt[
    event_time == 99, time_window_label := "Avg (+1 to +5)"
]
moved_tract_dt[
    event_time == 1, time_window_label := "Short Run (+1)"
]
moved_tract_dt[
    event_time == 96, time_window_label := "Long Run (+2 to +5)"
]
moved_tract_dt[
    ,
    time_window_label :=
        factor(
            time_window_label,
            levels = c("Avg (+1 to +5)", "Short Run (+1)", "Long Run (+2 to +5)") # nolint
        )
]

# Just to make it readable against to first, dark bar
errorbar_palette <-
    c(
        "#999999",
        "#000000"
    )

# Figure 5.2 -- wealth effects across time and by income quartile
# moved_tract

# Just to make it readable against to first, dark bar
errorbar_palette <- c("#999999",
                      "#000000",
                      "#000000")

figure_5_2 <-
    ggplot(
        data = moved_tract_dt,
        aes(
            x = factor(time_window_label),
            y = (estimate * 100000 * 100),
            fill = factor(sample_label)
        )
    ) +
    geom_bar(stat = "identity", position = position_dodge()) +
    geom_errorbar(aes(
        ymin = ((estimate - (1.64 * cluster_se)) * 100000 * 100),
        ymax = ((estimate + (1.64 * cluster_se)) * 100000 * 100),
        color = factor(sample_label)
    ),
    position = position_dodge(0.9),
    show.legend = FALSE,
    width = 0.2
    ) +
    theme_bw(base_size = 12) +
    theme(
        panel.grid.minor = element_blank(), # remove actual gridlines
        panel.grid.major = element_blank()
    ) +
    scale_y_continuous(breaks = (seq.int(from = 0, to = 7, by = 1))) +
    theme(
        legend.position = "bottom",
        legend.title = element_blank(),
        legend.key.size = unit(1.75, "lines")
    ) +
    labs(x = "Timing", y = "Effect per $100,000 of Lottery Winnings (pp)") +
    geom_vline(xintercept = 1.5, linetype = 3) +
    scale_fill_viridis_d() +
    scale_color_manual(values = errorbar_palette) +
    coord_cartesian(ylim = c(0, 7))

ggsave(
    plot = figure_5_2,
    filename = "~/paper/figures/figure-5.2.png",
    width = 6,
    height = 4.5,
    dpi = 600
)

ggsave(
    plot = figure_5_2,
    filename = "~/paper/figures/figure-5.2.tif",
    width = 6,
    height = 4.5,
    device = "tiff",
    dpi = 600
)

# Housekeeping
moved_tract_dt <- NULL
errorbar_palette <- NULL
figure_5_2 <- NULL
rm(
    moved_tract_dt,
    errorbar_palette,
    figure_5_2
)